<template>
  <el-dialog v-model="defectEditWinDialogVisible"
             title="修改常见缺陷" width="50%">
    <template #default>
      <el-form v-model="editDefect" label-width="auto">
        <el-form-item label="缺陷描述">
          <el-input v-model="editDefect.defectDescription"/>
        </el-form-item>
      </el-form>
      <el-form v-model="editDefect" inline="true">
        <el-form-item label="检测项类型">
          <el-input v-model="editDefect.defectType"/>
        </el-form-item>
        <el-form-item label="缺陷等级">
          <el-input v-model="editDefect.defectGrade"/>
        </el-form-item>
      </el-form>
    </template>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelEdit">取消</el-button>
        <el-button @click="editSubmit">确定</el-button>
      </div>
    </template>

  </el-dialog>
</template>
<script setup>
import {onMounted, ref , watch} from 'vue'
import {ElMessage} from "element-plus";
import defectRequest from "@/api/defect.js";

//声明这个子组件
const emits = defineEmits(['editCompletes','cancelEdit']);
//申明这个子组件的属性
const props = defineProps(['defectId']);
//弹窗标记
let defectEditWinDialogVisible = ref(false);
//编辑的项目对象
let editDefect = ref({});
// 当前登录用户的编号
let curUserId = ref();

//取消编辑按钮事件
function cancelEdit(){
  editDefect = ref({});
  defectEditWinDialogVisible.value = false;
  // 触发暴漏出去的取消编辑的事件
  emits("cancelEdit")

}
//提交事件
function editSubmit(){
  defectRequest.updateDefectById(editDefect.value,res=>{
    if(res.code == 200){
      ElMessage({
        type:"success",
        message:"编辑成功"
      });
      // 触发编辑完成的事件
      emits('editCompletes');
      // 关闭弹窗
      cancelEdit();
    }else{
      ElMessage({
        type:"warning",
        message:"项目编辑失败"
      });
    }
  })
}

//加载项目列表
function loadDefects(){
  defectRequest.selectById(props.defectId, res=>{
    if (res.data){
      //加载数据
      editDefect.value = res.data;
      console.log(editDefect.value);
      //显示弹窗
      defectEditWinDialogVisible.value = true;
    }
  })
}
//侦听器
watch(()=>props.defectId,()=>{
  //当props中的defectId发生变化时，就重新加载数据
  if(props.defectId){
    loadDefects();
  }
})

onMounted( ()=>{
  curUserId = localStorage.getItem("defectId");
})



</script>